/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */

/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */

var lowestCommonAncestor = function (root, p, q) {
    return dfs(root)
    function dfs(root) {
        if (!root || root === p || root === q) {
            return root
        }
        let leftNode = dfs(root.left)
        let rightNode = dfs(root.right)
        if (leftNode && rightNode) {
            return root
        }
        return leftNode || rightNode
    }
};